package com.reading.core.crawler.process;

import com.reading.common.entity.view.BookChapter;
import com.reading.core.crawler.constant.ProcessConstant;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author guwenqi_1995@163.com
 * @projectName alone-reading
 * @description: TODO
 * @date 2019/8/8 12:31
 */
@Slf4j
public class BiqutxtBookChapterProcess {

    /**
     * 笔趣阁域名
     */
    public static final String BIQUTXT_DOMAIN_NAME = "https://www.biqutxt.com";

    public static final String BIQUTXT_AUTHOR_LABEL = "作    者：";

    private static Integer startNum = 1;

    private static Integer endNum = 100;

    public static List<BookChapter> processBiqutxt(Integer bookId, String bookCode, Document html) throws IOException {
        List<BookChapter> chapters = new ArrayList<>();
        Elements chapterElements = html.getElementsByClass("_chapter");
        for (int i = startNum - 1; i < chapterElements.size() && i < endNum; i++) {
            Elements childChapters = chapterElements.get(i).getElementsByTag("li");
            for (Element childChapter : childChapters ) {
                BookChapter chapter = new BookChapter();
                chapter.setBookId(bookId);
                chapter.setChapterTitle(childChapter.text());

                String href = childChapter.html().substring(childChapter.html().indexOf("href=\"") + 6,
                        childChapter.html().indexOf("\">"));
                String chapterUrl = BIQUTXT_DOMAIN_NAME + bookCode + "/" + href;
                chapter.setContent(getContent(chapterUrl));

                chapters.add(chapter);
            }
        }

        return chapters;
    }

    public static String getContent(String chapterUrl) throws IOException {
        Document html = Jsoup.connect(chapterUrl).get();

        Element chapterContent = html.getElementById("content");

        log.info("content length ： {}", chapterContent.text().length());

        return chapterContent.text();
    }

    public static List<BookChapter> crawler(Integer bookId, String bookCode) throws IOException {
        String url = ProcessConstant.BIQUTXT_DOMAIN_NAME + bookCode;

        Document html = Jsoup.connect(url).get();
        List<BookChapter> chapters = processBiqutxt(bookId, bookCode, html);

        log.info("chapters size : {}", chapters.size());
        return chapters;
    }

    public static List<BookChapter> crawler(Integer bookId, String bookCode,
                                            Integer start, Integer end) throws IOException {
        startNum = start;
        endNum = end;

        return crawler(bookId, bookCode);
    }



    public static void main(String[] args) {
        String url = ProcessConstant.BIQUTXT_DOMAIN_NAME + "/0_312";

        try {
            Document html = Jsoup.connect(url).get();
            processBiqutxt(103, "/0_312", html);
        } catch (IOException e) {
            log.error("爬取 笔趣阁 图书失败", e);
        }
    }

}
